<?php

namespace App\Http\Controllers\Api\Admin;

use App\ActivityGroupPrice;
use App\Comment;
use App\CommentImg;
use App\Goods;
use App\GoodsBanner;
use App\GoodsImg;
use App\GoodSpu;
use App\ShowGoods;
use Illuminate\Http\Request;
use Hash;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    public function setGoods(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $ssn                       = $request->get('ssn');
        $name                      = $request->get('name');
        $mPrice                    = $request->get('m_price');
        $sPrice                    = $request->get('s_price');
        $imgId                     = $request->get('thumb_id');
        $info                      = $request->get('info');
        $isShop                    = $request->get('is_shop');
        $isRecommend               = $request->get('is_recommend');
        $isSelling                 = $request->get('is_selling');
        $isNew                     = $request->get('is_new');
        $sort                      = $request->get('sort');
        $classifyId                = $request->get('classify_id');
        $shopTime                  = $request->get("shop_time");
        $integral                  = $request->get('integral');
        $id                        = !is_null($request->get('id'))?$request->get('id'):0;
        $isFreeShipping            = $request->get('isFreeShipping');
        $exchangeIntegral          = $request->get('exchangeIntegral');
        $type                      = $request->get('type');
        $showGoodsOne              = $request->get('show_goods_one');

        $newGoods = Goods::whereId($id)->first();



        if(!$newGoods)
            $newGoods = new Goods();

        if(!is_null($ssn))$newGoods->ssn                        = $ssn;
        if(!is_null($name))$newGoods->name                      = $name;
        if(!is_null($mPrice))$newGoods->m_price                 = $mPrice;
        if(!is_null($sPrice))$newGoods->s_price                 = $sPrice;
        if(!is_null($imgId))$newGoods->thumb_id                 = $imgId;
        if(!is_null($info))$newGoods->info                      = $info;
        if(!is_null($isShop))$newGoods->is_shop                 = $isShop;
        if(!is_null($isRecommend))$newGoods->is_recommend       = $isRecommend;
        if(!is_null($isSelling))$newGoods->is_selling           = $isSelling;
        if(!is_null($isNew))$newGoods->is_new                   = $isNew;
        if(!is_null($sort))$newGoods->sort                      = $sort;
        if(!is_null($classifyId))$newGoods->classify_id         = $classifyId;
        if(!is_null($integral))$newGoods->integral              = $integral;
        if(!is_null($shopTime))$newGoods->shop_time             = $shopTime;
        if(!is_null($isFreeShipping))$newGoods->is_free_shipping= $isFreeShipping;
        if(!is_null($exchangeIntegral))$newGoods->exchange_integral=$exchangeIntegral;
        if(!is_null($type))$newGoods->type=$type;
        $newGoods->save();


        $newGoodsSpu=GoodSpu::whereGoodsId($newGoods->id)->get();

        if(count($newGoodsSpu)<1){
            $newGoodsSpu = new GoodSpu();
            $newGoodsSpu->goods_id      = $newGoods->id;
            $newGoodsSpu->spu_name      = '标准';
            $newGoodsSpu->price         = $newGoods->s_price;
            $newGoodsSpu->status        = 1;
            $newGoodsSpu->save();
        }


        $newGoods->link_url="/pages/goodsDetail/main?id=".$newGoods->id;
        $newGoods->save();



        $thisShowGoods = ShowGoods::whereId(1)->first();
        if(!$thisShowGoods)
            $thisShowGoods = new ShowGoods();

        if(!is_null($showGoodsOne))$thisShowGoods->show_goods_one = $showGoodsOne;
        $thisShowGoods->save();

        return response()->json(['level' => 'success','message' => '添加或修改成功',"data"=>$newGoods]);
    }

    public function delGoods(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $thatGoods =Goods::whereId($id)->first();

        if(!$thatGoods)
            return response()->json(["level"=>"error","message"=>"该商品不存在！"]);

        $thatGoods->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatGoods]);
    }
    
    public function setGoodsImg(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $goodsId = !is_null($request->get('good_id')) ? $request->get('good_id') : 0;
        $thumbId = !is_null($request->get('thumb_id')) ? $request->get('thumb_id') : 0;
        $id      = !is_null($request->get('id'))?$request->get('id'):0;


        $isGoods = Goods::whereId($goodsId)->first();
        if(!$isGoods)
            return response()->json(['level' => 'error','message' => '商品不存在']);

        $newGoodsImg = GoodsImg::whereId(trim($id))->first();
        if(!$newGoodsImg)
            $newGoodsImg = new GoodsImg();

        $newGoodsImg->good_id  = $goodsId;
        $newGoodsImg->thumb_id = $thumbId;
        $newGoodsImg->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newGoodsImg]);
    }

    public function delGoodsImg(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatGoodsImg =GoodsImg::whereId($id)->first();

        if(!$thatGoodsImg)
            return response()->json(["level"=>"error","message"=>"该商品不存在！"]);

        $thatGoodsImg->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatGoodsImg]);
    }

    public function setGoodsBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $goodsId = !is_null($request->get('good_id')) ? $request->get('good_id') : 0;
        $imgId   = !is_null($request->get('img_id')) ? $request->get('img_id') : 0;
        $sort    = !is_null($request->get('sort')) ? $request->get('sort') : 0;
        $id      = !is_null($request->get('id'))?$request->get('id'):0;


        $isGoods = Goods::whereId($goodsId)->first();
        if(!$isGoods)
            return response()->json(['level' => 'error','message' => '商品不存在']);

        $newGoodsBanner = GoodsBanner::whereId(trim($id))->first();
        if(!$newGoodsBanner)
            $newGoodsBanner = new GoodsBanner();

        $newGoodsBanner->good_id  = $goodsId;
        $newGoodsBanner->img_id   = $imgId;
        $newGoodsBanner->sort    = $sort;
        $newGoodsBanner->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newGoodsBanner]);
    }

    public function delGoodsBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatGoodsBanner =GoodsBanner::whereId($id)->first();

        if(!$thatGoodsBanner)
            return response()->json(["level"=>"error","message"=>"该商品不存在！"]);

        $thatGoodsBanner->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatGoodsBanner]);
    }

    public function delGoodsComment(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id =!is_null($request->get('id')) ? $request->get('id') : 0;

        $thisGoodsComment = Comment::whereId($id)->first();
        if(!$thisGoodsComment)
            return response()->json(['level' => 'error','message' => '该评论不存在']);

        $thisGoodsComment->delete();

        return response()->json(['level' => 'success','message' => '删除成功!','data' => $thisGoodsComment]);
    }

    public function delGoodsCommentImg(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thisGoodsCommentImg = CommentImg::whereId($id)->first();
        if(!$thisGoodsCommentImg)
            return response()->json(['level' => 'error','message' => '该图片不存在']);

        $thisGoodsCommentImg->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thisGoodsCommentImg]);
    }

    public function setGoodsSpu(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $spuName = $request->get('spu_name');
        $price = $request->get('price');
        $goodsId = $request->get("goods_id");
        $stock = $request->get('stock');
        $sort = $request->get('sort');
        $status = $request->get('status');
        $type = !is_null($request->get('type')) ? $request->get('type') : '';
        $activityGroupId = !is_null($request->get('activity_group_id')) ? $request->get('activity_group_id') : 0;
        $manyPrice = $request->get('many_price');
        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $newGoodsSpu = GoodSpu::whereId($id)->whereGoodsId($goodsId)->first();
        if(!$newGoodsSpu)
            $newGoodsSpu = new GoodSpu();

        if(!is_null($spuName))$newGoodsSpu->spu_name          = $spuName;
        if(!is_null($price))$newGoodsSpu->price               = $price;
        if(!is_null($goodsId))$newGoodsSpu->goods_id          = $goodsId;
        if(!is_null($stock))$newGoodsSpu->stock               = $stock;
        if(!is_null($sort))$newGoodsSpu->sort                 = $sort;
        if(!is_null($status))$newGoodsSpu->status             = $status;
        $newGoodsSpu->save();

        if($type == 'group'){
            $newActivityGroupPrice = ActivityGroupPrice::whereGoodsSpuId($newGoodsSpu->id)->whereGoodsId($newGoodsSpu->goods_id)->whereActivityGroupId($activityGroupId)->first();
            if(!$newActivityGroupPrice)
                $newActivityGroupPrice = new ActivityGroupPrice();

            $newActivityGroupPrice->activity_group_id      = $activityGroupId;
            $newActivityGroupPrice->goods_id               = $goodsId;
            $newActivityGroupPrice->goods_spu_id           = $newGoodsSpu->id;
            $newActivityGroupPrice->many_price             = $manyPrice;
            $newActivityGroupPrice->save();
        }

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newGoodsSpu]);
    }

    public function delGoodsSpu(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var GoodSpu $thisGoodsSpu */
        $thisGoodsSpu = GoodSpu::whereId($id)->first();
        if(!$thisGoodsSpu)
            return response()->json(['level' => 'error','message' => '该图片不存在']);

        /** @var ActivityGroupPrice $thisGroupPrice */
        $thisGroupPrice = ActivityGroupPrice::whereGoodsSpuId($thisGoodsSpu->id)->first();
        if($thisGroupPrice){
            $thisGroupPrice->delete();
        }

        $thisGoodsSpu->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thisGoodsSpu]);
    }
}
